package com.richest.api.controller.cn.merchant.merchant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richest.api.controller.form.MerchantRegisterForm;
import com.richest.api.service.IMerchantAnnouncementService;
import com.richest.api.service.IMerchantBaseService;
import com.richest.api.service.IMerchantBusinessService;
import com.richest.api.service.IMerchantContactService;
import com.richest.api.service.IMerchantQualificationService;
import com.richest.api.service.IMerchantService;
import com.richest.bean.MerchantAnnouncementBean;
import com.richest.bean.MerchantBaseBean;
import com.richest.bean.MerchantBean;
import com.richest.bean.MerchantBusinessBean;
import com.richest.bean.MerchantContactBean;
import com.richest.bean.MerchantQualificationBean;
import com.richest.util.CallResults;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * API接口 商户功能接口
 * 
 * @author 杨博
 * @version com.richest.api 2.0
 */
@RestController
@RequestMapping("/MerchantManagerController")
@Api(value = "商户功能接口")
@EnableAutoConfiguration
public class ApiMerchantManageController {

	@Autowired
	private IMerchantService merchantService;

	@Autowired
	private IMerchantAnnouncementService merchantAnnouncementService;

	@Autowired
	private IMerchantBaseService baseService;

	@Autowired
	private IMerchantBusinessService businessService;

	@Autowired
	private IMerchantContactService contactService;

	@Autowired
	private IMerchantQualificationService qualificationService;

	/**
	 * 商户注册功能接口
	 * 
	 * @param registerForm
	 *            商户注册表单
	 * @return 注册后生成商户id和管理员id
	 */
	@RequestMapping(value = "merchantRegister", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "商户注册", value = "商户注册", httpMethod = "POST")
	@ApiImplicitParam(name = "registerForm", value = "商户注册表单", required = true, paramType = "body", dataType = "MerchantRegisterForm")
	public CallResults merchantRegister(@RequestBody MerchantRegisterForm registerForm) {
		Map<String, Object> map = merchantService.merchantRegister(registerForm);
		boolean merchantOk = false;
		boolean employeeOk = false;

		for (Map.Entry<String, Object> entry : map.entrySet()) {
			if (entry.getKey().equals("MerchantId")) {
				if (entry.getValue() != null && (Long) entry.getValue() > 0) {
					merchantOk = true;
				}
			}
			if (entry.getKey().equals("EmployeeId")) {
				if (entry.getValue() != null && (Long) entry.getValue() > 0) {
					employeeOk = true;
				}
			}
		}

		if (merchantOk && employeeOk) {
			return CallResults.ok().put("Map", map);
		} else {
			return CallResults.error().put("message", "商户注册失败！");
		}
	}

	/**
	 * 设置商户公告信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "merchantAnnouncementSetting", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "设置商户公告信息", value = "设置商户公告信息", httpMethod = "POST")
	@ApiImplicitParam(name = "bean", value = "商户公告信息Bean", required = true, paramType = "body", dataType = "MerchantAnnouncementBean")
	public CallResults merchantAnnouncementSetting(@RequestBody MerchantAnnouncementBean bean) {
		int result = merchantAnnouncementService.merchantAnnouncementUpdate(bean);
		if (result == 1) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "受影响的行数：" + result);
		}
	}

	/**
	 * 获取商户公告信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "merchantAnnouncementSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取商户公告信息", value = "获取商户公告信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "商户公告信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults merchantAnnouncementSelectOneById(@RequestParam Long id) {
		MerchantAnnouncementBean bean = merchantAnnouncementService.merchantAnnouncementSelectOne(id);
		return CallResults.ok().put("MerchantAnnouncementBean", bean);
	}

	/**
	 * 设置商户基本信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "merchantBaseSetting", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "设置商户基本信息", value = "设置商户经营信息", httpMethod = "POST")
	@ApiImplicitParam(name = "bean", value = "商户经营信息Bean", required = true, paramType = "body", dataType = "MerchantBaseBean")
	public CallResults merchantBaseSetting(@RequestBody MerchantBaseBean bean) {
		int result = baseService.merchantBaseUpdate(bean);
		if (result == 1) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "受影响的行数：" + result);
		}
	}

	/**
	 * 获取商户基本信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "merchantBaseSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取商户基本信息", value = "获取商户基本信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "商户基本信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults merchantBaseSelectOneById(@RequestParam Long id) {
		MerchantBaseBean bean = baseService.merchantBaseSelectOne(id);
		return CallResults.ok().put("MerchantBaseBean", bean);
	}
	
	/**
	 * 设置商户经营信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "merchantBusinessSetting", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "设置商户经营信息", value = "设置商户经营信息", httpMethod = "POST")
	@ApiImplicitParam(name = "bean", value = "商户经营信息Bean", required = true, paramType = "body", dataType = "MerchantBusinessBean")
	public CallResults merchantBusinessSetting(@RequestBody MerchantBusinessBean bean) {
		int result = businessService.merchantBusinessUpdate(bean);
		if (result == 1) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "受影响的行数：" + result);
		}
	}

	/**
	 * 获取商户经营信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "merchantBusinessSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取商户经营信息", value = "获取商户经营信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "商户经营信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults merchantBusinessSelectOneById(@RequestParam Long id) {
		MerchantBusinessBean bean = businessService.merchantBusinessSelectOne(id);
		return CallResults.ok().put("MerchantBusinessBean", bean);
	}

	/**
	 * 设置商户联系信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "merchantContactUpdate", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "设置商户联系信息", value = "设置商户联系信息", httpMethod = "POST")
	@ApiImplicitParam(name = "bean", value = "商户联系信息Bean", required = true, paramType = "body", dataType = "MerchantContactBean")
	public CallResults merchantContactUpdate(@RequestBody MerchantContactBean bean) {
		int result = contactService.merchantContactUpdate(bean);
		if (result == 1) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "受影响的行数：" + result);
		}
	}

	/**
	 * 获取商户联系信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "merchantContactSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取商户联系信息", value = "获取商户联系信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "商户联系信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults merchantContactSelectOneById(@RequestParam Long id) {
		MerchantContactBean bean = contactService.merchantContactSelectOne(id);
		return CallResults.ok().put("MerchantContactBean", bean);
	}

	/**
	 * 设置商户资质信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "merchantQualificationUpdate", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "设置商户资质信息", value = "设置商户资质信息", httpMethod = "POST")
	@ApiImplicitParam(name = "bean", value = "商户资质信息Bean", required = true, paramType = "body", dataType = "MerchantQualificationBean")
	public CallResults merchantQualificationUpdate(@RequestBody MerchantQualificationBean bean) {
		int result = qualificationService.merchantQualificationUpdate(bean);
		if (result == 1) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "受影响的行数：" + result);
		}
	}

	/**
	 * 获取商户资质信息
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "merchantQualificationSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取商户资质信息", value = "获取商户资质信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "商户资质信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults merchantQualificationSelectOneById(@RequestParam Long id) {
		MerchantQualificationBean bean = qualificationService.merchantQualificationSelectOne(id);
		return CallResults.ok().put("MerchantQualificationBean", bean);
	}

	/**
	 * 通过id获取类别信息实体
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "merchantSelectOne", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取完整商户信息", value = "获取完整商户信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "企业信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults merchantSelectOne(@RequestParam Long id) {
		MerchantBean bean = merchantService.merchantSelectOne(id);
		return CallResults.ok().put("MerchantBean", bean);
	}

	/**
	 * 获取企业信息集合 通过参数设置分页大小，并取回指定分页
	 * 
	 * @author 杨博
	 * @email wsbc12345@163.com
	 * @param column
	 *            要匹配的字段名称
	 * @param value
	 *            要匹配的字段值
	 * @param page
	 *            页数
	 * @param size
	 *            页码
	 * @return 信息集合
	 */
	@RequestMapping(value = "merchantSelectMap", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取企业信息集合，参数设置匹配字段，参数设置匹配值，参数设置分页大小，参数指定获取指定分页", value = "获取企业信息集合，参数设置分页大小，参数指定获取指定分页", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "column", value = "参与匹配的字段名称", paramType = "query"),
			@ApiImplicitParam(name = "value", value = "参与匹配的字段值", paramType = "query"),
			@ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "Int"),
			@ApiImplicitParam(name = "size", value = "页码", paramType = "query", dataType = "Int") })
	public CallResults merchantSelectMap(@RequestParam String column, @RequestParam String value,
			@RequestParam Integer page, @RequestParam Integer size) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("column", column);
		map.put("value", value);
		map.put("beginNumber", (page - 1) * size);
		map.put("endNumber", size);
		List<Map<String, Object>> lst = new ArrayList<>();
		lst = merchantService.merchantSelectMap(map);
		return CallResults.ok().put("SelectMap", lst);
	}

}
